package com.ag.oa.core.controller;

import com.ag.oa.core.entity.vo.TaskVO;
import com.ag.oa.core.entity.vo.UserVO;
import com.ag.oa.core.service.ITaskService;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:   wjc
 * Date:     2021/10/25
 * Description: 任务，控制层
 */
@RestController("taskController")
@RequestMapping({"/task", "/test/task"})
public class TaskController {

    private static final Logger logger = LoggerFactory.getLogger(TaskController.class);

    @Autowired
    private ITaskService taskService;

    @GetMapping("/pageList")
    public String finTaskPageList(Integer projectId, Integer page, Integer pageSize) {

        JSONObject res = new JSONObject();
        int code = 200;
        try {
            List<TaskVO> taskList = taskService.getTaskPageList(projectId, page, pageSize);
            res.put("taskList", taskList);
        } catch (Exception e) {
            code = 500;
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", code);
        return res.toString();
    }

    @PostMapping("/createTask")
    public void createTask(@RequestBody TaskVO taskVO) {

        try {
            taskService.createTask(taskVO);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

    @GetMapping("/findTaskList")
    public List<TaskVO> findTaskList(String taskStatusStr, Integer projectId) {

        List<TaskVO> list = new ArrayList<>();
        try {
            // 转换类型
            String[] strArr = taskStatusStr.split(",");
            List<Integer> taskStatusList = new ArrayList<>();
            for (int i=0; i<strArr.length; i++) {
                taskStatusList.add(Integer.valueOf(strArr[i]));
            }

            TaskVO taskVO = new TaskVO();
            taskVO.setTaskStatusList(taskStatusList);
            taskVO.setProjectId(projectId);
            list = taskService.findTaskList(taskVO);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return list;
    }

    /**
     * 添加任务评论
     */
    @GetMapping(value = "/forum/save")
    public @ResponseBody String addSaveTaskForumMessage(Integer userId, Integer taskId, String contentStr) {

        JSONObject res = new JSONObject();
        int code = 200;

        try {
            taskService.saveNewTaskForum(userId, taskId, contentStr);
        }catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            code = 500;
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 获取 '我的任务' 列表数据
     * @param fromPage 页面来源, 0: 我的任务, 1: 工作周报内容添加时
     */
    @GetMapping(value = "/myTasks")
    public @ResponseBody String getUserTasks(Integer userId, Integer fromPage) {

        JSONObject res = new JSONObject();
        int code = 200;

        try {
            taskService.findUserTaskPageList(userId, fromPage, res);
        }catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            code = 500;
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

//    /**
//     * 获取个人任务所得的总积分, 并以所在的项目显示
//     */
//    @GetMapping(value = "/projectScore")
//    public @ResponseBody String getUserTaskScoreDetail(Integer userId) {
//
//        JSONObject res = new JSONObject();
//        int code = 200;
//
//        try {
//            List<Map> scoreList = taskService.getUserProjectTaskScoreList(userId);
//            res.put("projectScoreArr", scoreList);
//        }catch (Exception e) {
//            e.printStackTrace();
//            logger.error(e.getMessage(), e);
//            code = 500;
//        }
//        res.put("resCode", code);
//        return res.toJSONString();
//    }

    @GetMapping("/getTaskById")
    public TaskVO getTaskById(Integer taskId) {

        TaskVO vo = new TaskVO();
        try {
            vo = taskService.getTaskById(taskId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return vo;
    }

    @GetMapping("/getTaskUserStatus")
    public Map<String, Boolean> getTaskUserStatus(Integer taskId, Integer userId) {

        Map<String, Boolean> rMap = new HashMap<>();
        TaskVO vo = taskService.getTaskUserStatus(taskId, userId);

        rMap.put("isRecieve", vo.getIsRecieve()!=0);
        rMap.put("isComplete", vo.getCompleteStatus()!=0);

        // 任务中的职责 0：发起人 1：负责人 属于管理者类型， 2：普通成员
        if (vo.getTaskResp()==0 || vo.getTaskResp()==1) {
            rMap.put("isManager", true);
        } else {
            rMap.put("isManager", false);
        }
        return rMap;
    }

    @PostMapping("/updateTaskStatus")
    public void updateTaskStatus(@RequestBody TaskVO vo) {

        try {
            taskService.updateTaskStatus(vo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @PostMapping("/updateTaskDesc")
    public void updateTaskDesc(@RequestBody TaskVO vo) {

        try {
            taskService.updateTaskDesc(vo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @PostMapping("/createTaskUsers")
    public void createTaskUsers(@RequestBody TaskVO vo) {

        try {
            taskService.createTaskUsers(vo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @PostMapping("/updateCompleteTask")
    public void updateCompleteTask(@RequestBody TaskVO vo) {

        try {
            taskService.updateCompleteTask(vo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @GetMapping("/findTaskUserById")
    public List<TaskVO> findTaskUserById(Integer taskId, Integer completeStatus) {

        List<TaskVO> list = new ArrayList<>();
        try {
            list = taskService.findTaskUserById(taskId, completeStatus);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return list;
    }

    @GetMapping("/getTaskUserCheckDataList")
    public List<UserVO> getTaskUserCheckDataList(String username, Integer taskId, Integer limitCount) {

        List<UserVO> list = new ArrayList<>();
        try {
            list = taskService.getTaskUserCheckDataList(username, taskId, limitCount);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return list;
    }

    @PostMapping("/updateUserMixedStatus")
    public void updateUserMixedStatus(@RequestBody TaskVO vo) {

        try {
            taskService.updateUserMixedStatus(vo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @GetMapping("/getNewRunTaskTop10")
    public List<TaskVO> getNewRunTaskTop10() {

        List<TaskVO> parentTaskArr = new ArrayList<>();
        try {
            parentTaskArr = taskService.getNewRunTaskTop10();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return parentTaskArr;
    }

}
